<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>子组件向父组件传值</title>
    <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
    <style>
      .parent {
        width: 600px;
        min-height: 300px;
        background-color: pink;
        padding: 20px;
      }
      .child {
        width: 80%;
        min-height: 100px;
        height: auto;
        padding: 0px 10px;
        border: 1px solid black;
        background-color: lightgreen;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <parent-component></parent-component>
    </div>
    <script>
      //1.声明父组件
      Vue.component("parent-component", {
        data() {
          return { lists: ["haoren", "lucy", "lily"],fontSize:16 };
        },
        template: `<div class='parent'>
                     <h3>父组件</h3>
                     <child-component  @large-text='handle($event)'   :array='lists'></child-component>
                     <hr>
                     <ul :style='{fontSize:fontSize+"px"}'>
                       <li :key='index' v-for='(v,index) in lists'>{{v}}</li>
                     </ul>
                   </div>`,
        methods:{
          handle(val){
            console.log('监听到自定义的large-text事件');
            console.log(val);
            this.fontSize+=val;
            
          }
        }           
      });

      //2.声明子组件
      Vue.component("child-component", {
        props: ["array"],
        data() {
          return { selfData: "子组件的数据",step:7 };
        },
        template: `<div class='child'>
                    <h3>子组件</h3>
                    <button @click='$emit("large-text",5)' >扩大父组件的字体(5)</button>
                    <button @click='$emit("large-text",10)' >扩大父组件的字体(10)</button>
                    <button @click='$emit("large-text",step)' >扩大父组件的字体({{step}})</button>
                    <hr>
                     <ul>
                       <li :key='index' v-for='(v,index) in array'>{{v}}</li>
                     </ul>
                   </div>`,
      });

      //1. 实例化一个Vue的实例
      let vm = new Vue({
        el: "#app",
        data: {},
      });
    </script>
  </body>
</html>
